import { config } from '../../config/index';
import { request } from '../../utils/request';

/** 获取点评列表 */
function mockFetchReviewsList(pageIndex = 1, pageSize = 20) {
  const { delay } = require('../_utils/delay');
  const { getReviewsList } = require('../../model/reviews');

  return delay().then(() =>
    getReviewsList(pageIndex, pageSize).map((item) => {
      return {
        spuId: item.reviewsId,
        thumb: item.primaryImage,
        title: item.title,
        avatar: item.avatar,
        author: item.author,
        mark: item.mark,
        issue: item.releaseTime,
        hot: item.isHot,
        likerNum: item.likerNum,
      };
    }),
  );
}

/** 获取点评列表 */
export function fetchReviewsList(pageIndex = 1, pageSize = 10, isHot = false) {
  if (config.useMock) {
    return mockFetchReviewsList(pageIndex, pageSize);
  }
  const query_params = {
    keyword: "",
    user_id: getUserId(),
    group_id: "",
    is_hot: isHot,
    page_num: pageIndex,
    page_size: pageSize
  };
  return request('reviews/list', query_params, 'POST').then((res) => {
    return res;
  });
}

export function searchReviewsList(keyword = "", pageIndex = 1, pageSize = 10) {
  const query_params = {
    keyword: keyword,
    user_id: getUserId(),
    group_id: "",
    is_hot: isHot,
    page_num: pageIndex,
    page_size: pageSize
  }
  return request('reviews/list', query_params, 'POST').then((res) => {
    return res;
  });
}

function getUserId() {
  return wx.getStorageSync('user_id');
}
